package com.comment.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.smartstruts.action.Action;
import org.smartstruts.action.ActionForm;

import com.comment.domain.Account;
import com.comment.domain.Comment;
import com.comment.form.CommentForm;
import com.comment.service.CommentService;
import com.comment.util.Constants;

/**
 * 添加一条帖子类
 * 
 * @author Wangpeihu
 * @since 07/01/2010
 */
public class AddCommentAction extends Action {
	CommentService cs = new CommentService();

	@Override
	public String execute(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("utf-8");
		CommentForm cf = (CommentForm) form;

		Comment comment = new Comment();
		HttpSession session = request.getSession();
		Account account = (Account) session.getAttribute(Constants.ACCOUNT_KEY);
		comment.setAccount(account);
		comment.setTime(new Date());
		comment.setTitle(cf.getTitle());
		comment.setContent(cf.getContent());

		/** 如果标题或者内容为空，那就不添加 */
		if (null != comment.getTitle() && !"".equals(comment.getTitle().trim())
				&& null != comment.getContent()
				&& !"".equals(comment.getContent().trim())) {
			cs.insertComment(comment);
		}
		return "success";
	}
}